﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
using CrawlerCore;
using HtmlAgilityPack;
using WapCrawlerLib;

namespace WapCrawlerCore.WapCrawler
{
    public class WapTaiGameWapSite : WapSiteBase
    {
        public WapTaiGameWapSite(int wapCrawlerId)
            : base(wapCrawlerId)
        {
            
        }

        #region IWapSite Members

        public override void StartCrawler()
        {
            //Get items of first page
            GetPages(objWapCrawler.wapSiteUrl, objWapCrawler.recentCrawlerUrl);
        }

        public override void GetPages(string wapUrl, string crawlerUrl)
        {
            string pageUrl = GeneralClass.GetFullUrl(wapUrl, string.Format(crawlerUrl, 1));
            HtmlDocument doc = Browser.Get(pageUrl);
            HtmlNodeCollection nodeCollection = doc.DocumentNode.SelectNodes("//a[@class=\"post-count-link\"]");
            if (nodeCollection != null)
            {
                //remove first node
                nodeCollection.Remove(0);

                foreach(HtmlNode node in nodeCollection)
                {
                    //infor of a item
                    string nextPageUrl = node.Attributes["href"].Value;
                    nextPageUrl = nextPageUrl.Replace(objWapCrawler.wapSiteUrl, "");
                    objWapCrawler.recentCrawlerUrl = nextPageUrl;
                    _dc.SubmitChanges();
                    GetItemsOfPage(nextPageUrl);
                }
            }
        }



        public override string GetItemsOfPage(string pageUrl)
        {
            HtmlDocument doc = Browser.Get(objWapCrawler.wapSiteUrl);
            HtmlNodeCollection nodeCollection = doc.DocumentNode.SelectNodes("//ul[@class=\"posts\"]/li/a");
            if (nodeCollection != null)
            {
                Parallel.ForEach(nodeCollection, node =>
                {
                    //infor of a item
                    string href = node.Attributes["href"].Value;
                    string title = node.InnerHtml;
                    GetItem(href, title, "", "");

                });
            }
            return "";
        }

        public override void GetItem(string itemUrl, string title, string headContent, string thumbnailUrl)
        {
            WapDataClassesDataContext dc = new WapDataClassesDataContext();
            
            //Lấy topic của bài post
            TopicCrawler objTopic = dc.TopicCrawlers.Single(p => p.topicValue == 1 && p.wapCrawlerId == objWapCrawler.wapCrawlerId);
            //Tìm xem bài post này đã tồn tại trong CSDL chưa
            PostCrawler objPost = new PostCrawler();
            try
            {
                objPost = dc.PostCrawlers.Single(p => p.postUrl == itemUrl);
            }
            catch
            {
                objPost.postUrl = itemUrl.Replace(objWapCrawler.wapSiteUrl, ""); ;
                objPost.postTitle = title;
                dc.PostCrawlers.InsertOnSubmit(objPost);
                dc.SubmitChanges();
            }

            //Tạo liên kết topic id với post id
            TopicPostCrawler objTopicPost = new TopicPostCrawler();
            try
            {
                objTopicPost = dc.TopicPostCrawlers.Single(p => p.topicCrawlerId == objTopic.topicCrawlerId &&
                                                                p.postCrawlerId == objPost.postCrawlerId);
            }
            catch
            {
                objTopicPost.topicCrawlerId = objTopic.topicCrawlerId;
                objTopicPost.postCrawlerId = objPost.postCrawlerId;
                dc.TopicPostCrawlers.InsertOnSubmit(objTopicPost);
                dc.SubmitChanges();
            }

            //Load page of a item
            HtmlDocument itemDoc = Browser.Get(GeneralClass.GetFullUrl(objWapCrawler.wapSiteUrl, itemUrl));
            if (null == itemDoc)
            {
                return;
            }

            //Note1: get content of a item
            HtmlNodeCollection nodeCollection = itemDoc.DocumentNode.SelectNodes(".//div[@class=\"post-body entry-content\"]");
            if (nodeCollection != null)
            {
                string postContent = nodeCollection[0].InnerHtml;
                //Set false để chỉnh sửa lại nội dung, sau khi chỉnh xong set lại true
                objPost.crawlerStatus = false;

                //Remove facebook tags
                HtmlNodeCollection removedNodeCollection = itemDoc.DocumentNode.SelectNodes(".//div[@class=\"post-body entry-content\"]/div[contains(@style,\"-moz-background-clip\")]");
                if (removedNodeCollection != null){
                    int index = postContent.IndexOf(removedNodeCollection[0].InnerHtml);
                    postContent = postContent.Remove(index);
                }

                //Get img url
                HtmlNodeCollection imgNodeCollection = nodeCollection[0].SelectNodes(".//img");
                if (imgNodeCollection != null)
                {
                    //foreach (HtmlNode imgNode in imgNodeCollection)
                    for (int i = 0; i < imgNodeCollection.Count; i++)
                    {
                        HtmlNode imgNode = imgNodeCollection[i];
                        string imgScr = imgNode.Attributes["src"].Value;
                        postContent = postContent.Replace(imgNode.OuterHtml, string.Format("[IMGTAG{0}]",i+1));
                        ImageCrawler img = new ImageCrawler();
                        try
                        {
                            img = dc.ImageCrawlers.Single(p => p.imageUrl == imgScr && p.postCrawlerId == objPost.postCrawlerId);
                        }
                        catch
                        {
                            img.imageUrl = imgNode.Attributes["src"].Value;
                            objPost.ImageCrawlers.Add(img);
                        }
                    }
                }

                //ignore first item and last item (it's a main page)
                //Remand items are files
                //Get img url
                HtmlNodeCollection fileNodeCollection = nodeCollection[0].SelectNodes(".//a");
                if (fileNodeCollection != null)
                {
                    //foreach (HtmlNode fileNode in fileNodeCollection)
                    for (int i = 0; i < fileNodeCollection.Count; i++)
                    {
                        HtmlNode fileNode = fileNodeCollection[i];
                        if (!postContent.Contains(fileNode.OuterHtml))
                            continue;

                        string fileUrl = "";
                        string fileName = fileNode.InnerHtml;
                        string href = fileNode.Attributes["href"].Value;

                        //Kiểm tra xem có phải chính xác là file jar hoặc jad không (đuôi url sẽ là .jar hoặc .jad)
                        Regex r = new Regex(@"\.(jar|jad)$");
                        Match m = r.Match(href);
                        if (m.Success)
                        {
                            fileUrl = href;
                            postContent = postContent.Replace(fileNode.OuterHtml, string.Format("[FILETAG{0}]",i+1));
                        }
                        else
                        {
                            //fileUrl = "";
                            int count = 1;
                            do
                            {
                                Browser.Get(href);
                                href = Browser.Response.Headers["location"];
                                m = r.Match(href);
                                if (m.Success)
                                {
                                    fileUrl = href;
                                    postContent = postContent.Replace(fileNode.OuterHtml, string.Format("[FILETAG{0}]", i + 1));
                                    FileCrawler objFileCrawler = new FileCrawler();
                                    //Xác định file đó đã lưu trong csdl chưa
                                    try
                                    {
                                        objFileCrawler = dc.FileCrawlers.Single(p => p.fileUrl == fileUrl
                                            && p.postCrawlerId == objPost.postCrawlerId);

                                    }
                                    catch
                                    {
                                        //Nếu chưa lưu trong csdl thì thêm vào
                                        objFileCrawler.fileName = fileName;
                                        objFileCrawler.fileUrl = fileUrl;
                                        objPost.FileCrawlers.Add(objFileCrawler);
                                    }
                                }
                            } while (count++ < 3 && fileUrl == string.Empty);
                        }
                    }
                }

                objPost.postContent = postContent;
            }

            //Crawler a post complete!
            objPost.crawlerStatus = true;
            dc.SubmitChanges();
        }
        #endregion
    }
}